/**
 * Copyright (c) 2015-2017, Henry Yang 杨勇 (gismail@foxmail.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mapfinal.server.layer;

import com.jfinal.config.Routes;
import com.jfinal.kit.StrKit;
import com.jfinal.template.Engine;
import com.lambkit.Lambkit;
import com.lambkit.common.service.ServiceManager;
import com.lambkit.core.rpc.RpcConfig;
import com.lambkit.db.datasource.ActiveRecordPluginWrapper;
import com.lambkit.module.LambkitModule;

import com.mapfinal.server.MapfinalServerConfig;
import com.mapfinal.server.layer.model.MfDatastore;
import com.mapfinal.server.layer.model.MfLayer;
import com.mapfinal.server.layer.model.MfStyle;
import com.mapfinal.server.layer.model.MfWorkspace;
import com.mapfinal.server.layer.service.impl.MfDatastoreServiceImpl;
import com.mapfinal.server.layer.service.impl.MfDatastoreServiceMock;
import com.mapfinal.server.layer.service.impl.MfLayerServiceImpl;
import com.mapfinal.server.layer.service.impl.MfLayerServiceMock;
import com.mapfinal.server.layer.service.impl.MfStyleServiceImpl;
import com.mapfinal.server.layer.service.impl.MfStyleServiceMock;
import com.mapfinal.server.layer.service.impl.MfWorkspaceServiceImpl;
import com.mapfinal.server.layer.service.impl.MfWorkspaceServiceMock;
import com.mapfinal.server.layer.web.MfDatastoreDirective;
import com.mapfinal.server.layer.web.MfDatastoreMarker;
import com.mapfinal.server.layer.web.MfLayerDirective;
import com.mapfinal.server.layer.web.MfLayerMarker;
import com.mapfinal.server.layer.web.MfStyleDirective;
import com.mapfinal.server.layer.web.MfStyleMarker;
import com.mapfinal.server.layer.web.MfWorkspaceDirective;
import com.mapfinal.server.layer.web.MfWorkspaceMarker;

/**
 * @author yangyong 
 * @website: www.lambkit.com
 * @email: gismail@foxmail.com
 * @date 2021-09-06
 * @version 1.0
 * @since 1.0
 */
public class ServerLayerModule extends LambkitModule  {

	@Override
	public void configMapping(ActiveRecordPluginWrapper arp) {
		if(StrKit.isBlank(getConfig().getDbConfig())) {
			mapping(arp);
		}
	}
	
	@Override
	public void configMapping(String name, ActiveRecordPluginWrapper arp) {
		super.configMapping(name, arp);
		if(StrKit.notBlank(name) && name.equals(getConfig().getDbConfig())) {
			mapping(arp);
		}
	}
	
	@Override
	public void configEngine(Engine me) {
		addDirective(me, "mfLayer", MfLayerDirective.class);
		addDirective(me, "mfDatastore", MfDatastoreDirective.class);
		addDirective(me, "mfWorkspace", MfWorkspaceDirective.class);
		addDirective(me, "mfStyle", MfStyleDirective.class);
	}
	
	@Override
	public void configRoute(Routes me) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		addTag(this);
		if("server".equals(getConfig().getRpcType())) {
			registerLocalService();
		} else if("client".equals(getConfig().getRpcType())) {
			registerRemoteService();
		} 
	}
	
	public void mapping(ActiveRecordPluginWrapper arp) {
		arp.addMapping("mf_layer", "id", MfLayer.class);
		arp.addMapping("mf_datastore", "id", MfDatastore.class);
		arp.addMapping("mf_workspace", "name", MfWorkspace.class);
		arp.addMapping("mf_style", "id", MfStyle.class);
	}
	
	public void addTag(LambkitModule lk) {
		lk.addTag("mfLayer", new MfLayerMarker());
		lk.addTag("mfDatastore", new MfDatastoreMarker());
		lk.addTag("mfWorkspace", new MfWorkspaceMarker());
		lk.addTag("mfStyle", new MfStyleMarker());
	}
			
	public void registerLocalService() {
		registerLocalService(getRpcGroup(), getRpcVersion(), getRpcPort());
	}
	
	public void registerLocalService(String group, String version, int port) {
		ServiceManager.me().mapping(MfLayerService.class, MfLayerServiceImpl.class, MfLayerServiceMock.class, group, version, port);
		ServiceManager.me().mapping(MfDatastoreService.class, MfDatastoreServiceImpl.class, MfDatastoreServiceMock.class, group, version, port);
		ServiceManager.me().mapping(MfWorkspaceService.class, MfWorkspaceServiceImpl.class, MfWorkspaceServiceMock.class, group, version, port);
		ServiceManager.me().mapping(MfStyleService.class, MfStyleServiceImpl.class, MfStyleServiceMock.class, group, version, port);
	}
	
	public void registerRemoteService() {
		registerRemoteService(getRpcGroup(), getRpcVersion(), getRpcPort());
	}
	
	public void registerRemoteService(String group, String version, int port) {
		ServiceManager.me().remote(MfLayerService.class, MfLayerServiceMock.class, group, version, port);
		ServiceManager.me().remote(MfDatastoreService.class, MfDatastoreServiceMock.class, group, version, port);
		ServiceManager.me().remote(MfWorkspaceService.class, MfWorkspaceServiceMock.class, group, version, port);
		ServiceManager.me().remote(MfStyleService.class, MfStyleServiceMock.class, group, version, port);
	}
	
	public int getRpcPort() {
		return Lambkit.config(RpcConfig.class).getDefaultPort();
	}
	public String getRpcGroup() {
		return Lambkit.config(RpcConfig.class).getDefaultGroup();
	}
	public String getRpcVersion() {
		return getConfig().getRpcVersion();
	}
	public MapfinalServerConfig getConfig() {
		return Lambkit.config(MapfinalServerConfig.class);
	}
}

